home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Technology Seed / August 1998 ADC Seed CD.toast / Language Analysis Manager / DarumaDR1Package / Examples / TextConverter / Sources / Application.c next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.1 KB  |  206 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Application.c
  3.     
  4.     Contains:    A Sample application for High-level text conversion
  5.  
  6.      Version:    Technology:    System 8
  7.                  Release:    Daruma Developer Release 1
  8.  
  9.      Copyright:    1998 by Apple Computer, Inc., all rights reserved
  10.  
  11.      Contact:    daruma@apple.com
  12.  
  13. */
  14.  
  15.  
  16. #include "HighLevelTextConvert.h"
  17. #include "Application.h"
  18.  
  19. #include <Dialogs.h>
  20.  
  21.  
  22. //========================================================================================
  23. // Prototypes for static functions
  24. //========================================================================================
  25. static void ToolboxInitialize( void );
  26. static void DoMainDialog( void );
  27. static Handle GetDialogItemHandle( DialogRef dialog, short itemNo );
  28. static void ShowErrorAlert ( OSStatus error );
  29. static void ChangeDefautItem( DialogRef dialog, SInt16 newDefaultItem );
  30.  
  31.  
  32. /* =======================================================================================
  33.     main
  34.    ======================================================================================= */
  35. void main ( void )
  36. {
  37.     ToolboxInitialize();
  38.     DoMainDialog();
  39. }
  40.  
  41.  
  42. /* =======================================================================================
  43.     ToolboxInitialize
  44.    ======================================================================================= */
  45. static void ToolboxInitialize( void )
  46. {
  47.     InitGraf( (Ptr)&qd.thePort);
  48.     InitFonts();
  49.     FlushEvents( everyEvent, 0);
  50.     InitWindows();
  51.     InitMenus();
  52.     TEInit();
  53.     InitDialogs( nil);
  54.     InitCursor();
  55. }
  56.  
  57.  
  58. /* =======================================================================================
  59.     DoMainDialog
  60.    ======================================================================================= */
  61. static void DoMainDialog( void )
  62. {
  63.     OSStatus        err;
  64.     DialogRef        dialog;
  65.     GrafPtr            savePort;
  66.     short            itemHit = 0;
  67.     Str255            srcStr, dstStr;
  68.     ByteCount        actualLength;
  69.     
  70.     GetPort( &savePort);
  71.     
  72.     dialog = GetNewDialog( kMainDialogResID, NULL, (WindowRef)-1L);
  73.     if ( dialog == NULL) goto errExit;
  74.     
  75.     SetPort( dialog);
  76.     
  77.     ChangeDefautItem( dialog, kConvToKanjiBtnItem);
  78.     
  79.     while ( itemHit != kQuitBtnItem)
  80.     {
  81.         ModalDialog( NULL, &itemHit);
  82.         
  83.         if ( itemHit == kConvToKanjiBtnItem)
  84.         {
  85.             GetDialogItemText( GetDialogItemHandle( dialog, kYomiEditTextItem), srcStr);
  86.             
  87.             err = LaConvertPascalString( kConvertKanaToKanji, srcStr, &actualLength, dstStr);
  88.             if ( err != noErr) goto errExit;
  89.             
  90.             SetDialogItemText( GetDialogItemHandle( dialog, kKanjiEditTextItem), dstStr);
  91.         }
  92.         else if ( itemHit == kConvToYomiBtnItem)
  93.         {
  94.             GetDialogItemText( GetDialogItemHandle( dialog, kKanjiEditTextItem), srcStr);
  95.             
  96.             err = LaConvertPascalString( kConvertKanjiToKana, srcStr, &actualLength, dstStr);
  97.             if ( err != noErr) goto errExit;
  98.             
  99.             SetDialogItemText( GetDialogItemHandle( dialog, kYomiEditTextItem), dstStr);
  100.         }
  101.         else if ( itemHit == kYomiEditTextItem)
  102.         {
  103.             ChangeDefautItem( dialog, kConvToKanjiBtnItem);
  104.         }
  105.         else if ( itemHit == kKanjiEditTextItem)
  106.         {
  107.             ChangeDefautItem( dialog, kConvToYomiBtnItem);
  108.         }
  109.     }
  110.     
  111. errExit:;
  112.     SetPort( savePort);
  113.     
  114.     if ( dialog != NULL)
  115.         DisposeDialog( dialog);
  116.     else
  117.         ShowErrorAlert( err);
  118.     
  119.     LaFinalizeConvert();
  120. }
  121.  
  122.  
  123. /* =======================================================================================
  124.     ShowErrorAlert
  125.    ======================================================================================= */
  126. static void ShowErrorAlert ( OSStatus error )
  127. {
  128.     Str31        errNumStr;
  129.     
  130.     if ( error == noErr) return;
  131.     
  132.     NumToString( error, errNumStr);
  133.     ParamText( errNumStr, "\p", "\p", "\p");
  134.     
  135.     Alert( kErrorAlertResID, NULL);
  136. }
  137.  
  138.  
  139. /* =======================================================================================
  140.     GetDialogItemHandle
  141.    ======================================================================================= */
  142. static Handle GetDialogItemHandle( DialogRef dialog, short itemNo )
  143. {
  144.     short    itemType;
  145.     Handle    itemHandle;
  146.     Rect    box;
  147.     
  148.     GetDialogItem( dialog, itemNo, &itemType, &itemHandle, &box);
  149.     return itemHandle;
  150. }
  151.  
  152.  
  153. /* =======================================================================================
  154.     ChangeDefautItem
  155.    ======================================================================================= */
  156. static void ChangeDefautItem( DialogRef dialog, SInt16 newDefaultItem )
  157. {
  158.     GrafPtr            savePort;
  159.     short            itemType;
  160.     Handle            itemHandle;
  161.     Rect            itemRect;
  162.     SInt16            curDefaultItem = GetDialogDefaultItem( dialog);
  163.     RgnHandle        targetRgn, tempRgn;
  164.  
  165.     if ( newDefaultItem != curDefaultItem)
  166.     {
  167.         GetPort( &savePort);
  168.         SetPort( dialog);
  169.         
  170.         SetDialogDefaultItem( dialog, newDefaultItem);
  171.         
  172.         targetRgn = NewRgn();
  173.         tempRgn = NewRgn();
  174.         
  175.         GetDialogItem( dialog, curDefaultItem, &itemType, &itemHandle, &itemRect);
  176.         RectRgn( tempRgn, &itemRect);
  177.         InsetRect( &itemRect, -6, -6);
  178.         RectRgn( targetRgn, &itemRect);
  179.         DiffRgn( targetRgn, tempRgn, targetRgn);
  180.         EraseRgn( targetRgn);
  181.  
  182.         GetDialogItem( dialog, newDefaultItem, &itemType, &itemHandle, &itemRect);
  183.         RectRgn( tempRgn, &itemRect);
  184.         InsetRect( &itemRect, -6, -6);
  185.         RectRgn( targetRgn, &itemRect);
  186.         DiffRgn( targetRgn, tempRgn, targetRgn);
  187.         InvalRgn( targetRgn);
  188.  
  189.         DisposeRgn( targetRgn);
  190.         DisposeRgn( tempRgn);
  191.  
  192.         SetPort( savePort);
  193.     }
  194. }
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.